<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="关键字">
              <a-input placeholder="请输入" v-model="queryParam.key"></a-input>
            </a-form-item>
          </a-col>
        
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
<!--      <a-button type="primary" icon="download" @click="handleExportXls('Linker员工信息表')">导出</a-button>-->
<!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">-->
<!--        <a-button type="primary" icon="import">导入</a-button>-->
<!--      </a-upload>-->
<!--      <a-dropdown v-if="selectedRowKeys.length > 0">-->
<!--        <a-menu slot="overlay">-->
<!--          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>-->
<!--        </a-menu>-->
<!--        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>-->
<!--      </a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="showChangePassword(record)">修改密码</a>

          
          <a @click="handleEdit(record)">编辑</a>

          
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->
    <a-modal v-model="changePasswordModal" title="修改密码" @ok="handleOk">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="旧密码">
          <a-input type="password" placeholder="请输入" v-decorator="['oldPassword', {rules:[{required:true,message:'请输入'}]}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="新密码">
          <a-input type="password" placeholder="请输入" v-decorator="['newPassword', {rules:[{required:true,message:'请输入'}]}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="确认新密码">
          <a-input type="password" placeholder="请输入" v-decorator="['newPasswordConfirm', {rules:[{required:true,message:'请输入'}]}]" />
        </a-form-item>
      </a-form>
    </a-modal>
    <!-- 表单区域 -->
    <authUser-modal ref="modalForm" @ok="modalFormOk"></authUser-modal>
  </a-card>
</template>

<script>
  import '@/assets/less/TableExpand.less'
  import AuthUserModal from './modules/AuthUserModal'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import pick from 'lodash.pick'
  export default {
    name: "AuthUserList",
    mixins:[JeecgListMixin],
    components: {
        AuthUserModal
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        form: this.$form.createForm(this),
        rowObj:{},
        changePasswordModal:false,
        description: 'Linker员工信息表页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
           },
		   {
            title: '工号',
            align:"center",
            dataIndex: 'StaffNo',
            isKey:true
           },
		   {
            title: '姓名',
            align:"center",
            dataIndex: 'Name',
            isKey:true
           },
		   {
            title: '性别',
            align:"center",
            dataIndex: 'Gender_Value'
           },
		   {
            title: '出生日期',
            align:"center",
            dataIndex: 'Birthday',
            customRender:function (t,r,index) {
              return t?t.split(' ')[0]:'';
            }
           },
		   {
            title: '手机',
            align:"center",
            dataIndex: 'Tel',
            isKey:true
           },
		   {
            title: '职务',
            align:"center",
            dataIndex: 'Position',
            isKey:true
           },
		   {
            title: '入职时间',
            align:"center",
            dataIndex: 'JoinedDate',
            customRender:function (t,r,index) {
              return t?t.split(' ')[0]:'';
            }
           },
		   {
            title: '在职状态',
            align:"center",
            dataIndex: 'WorkStatus_Value'
           },
           {
            title: '用户角色',
            align:"center",
            dataIndex: 'UserRole_Value'
           },
		   {
            title: '是否User',
            align:"center",
            dataIndex: 'IsUser',
            customRender:function (t,r,index) {
              return t == '1'?'是':t == '0'?'否':'';
            }
           },
           {
            title: '最近更新人',
            align:"center",
            dataIndex: 'UpdateBy_Name'
           },
       {
            title: '最近更新日期',
            align:"center",
            dataIndex: 'Ts',
            customRender:function (t,r,index) {
              return t?new Date(parseInt(t)).toLocaleDateString().replace('/','-').replace('/','-'):'';
            }
            
           },
          {
            title: '操作',
            dataIndex: '',
            align:"center",
            scopedSlots: { customRender: 'action' },
          }
        ],
		url: {
          list: "/user/queryRows",
          delete: "/user/delete",
          deleteBatch: "/robot/authUser/deleteBatch",
          exportXlsUrl: "robot/authUser/exportXls",
          importExcelUrl: "robot/authUser/importExcel",
       },
    }
  },
  computed: {
    importExcelUrl: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    }
  },
    methods: {
      showChangePassword(record){
        this.rowObj = record;
        this.changePasswordModal = true;
        this.form.resetFields();
        this.$nextTick(() => {
          this.form.setFieldsValue(pick({},'oldPassword','newPassword','newPasswordConfirm'))
        })
      },
      handleOk(){
        let userId = this.rowObj.Id;
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            let formData = Object.assign({}, values);

            if(formData.newPasswordConfirm !== formData.newPassword){
              this.$message.error('两次新密码不一致');
              return false;
            }

            formData.userId = userId;
            let httpurl = '/auth/changePassword';
            this.$http.postAction(httpurl,formData).then((res)=>{
              if(res.status == 1){
                that.$message.success(res.message);
                that.changePasswordModal = false;
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              // that.close();
            })
          }
        })
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>